<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/12/24 0024
  Time: 14:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>生产派工单登记子页面</title>
    <link rel="stylesheet"  href="/bootstrap/bootstrap-5.3.3-dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/css/manufactureCss/e/production_dispatch_order_review_sub.css"></link>
    <style type="text/css">
        .col1{
            width: 11%;
        }
        .col2{
            width: 30%;
        }
        .num{
            width: 10%;
        }
    </style>
</head>
<body style="padding: 15px 50px;background-color: #eeeeee;">
<div class="panel-heading hhead">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <span style="color: blue;">您正在做的业务是：</span>
            <li class="breadcrumb-item"><a href="#">生产管理</a></li>
            <li class="breadcrumb-item"><a href="#">内部生产管理</a></li>
            <li class="breadcrumb-item active" aria-current="page">生产登记（责任人控制）</li>
        </ol>
    </nav>
</div>
<div style="margin-bottom: 2px">
    <div style="display: inline;margin-left: 95%;">
        <button class="btn btn-primary">返回</button>
    </div>
</div> <!-- 此div改用栅格来做 -->
<div id="designListBox">
    <div style="text-align: center">
        <h3>生产派工单</h3>
    </div>
    <div style="padding: 30px">
        <table class="tbl1">
            <tr>
                <td class="col1">派工单编号：</td>
                <td class="col2" id="manufactureId"></td>
                <td class="col1">出库单编号：</td>
                <td class="col2" id=""></td>
            </tr>
            <tr>
                <td class="col1">产品编号：</td>
                <td class="col2" id="productId"></td>
                <td class="col1">产品名称：</td>
                <td class="col2" id="productName"></td>
            </tr>
            <tr>
                <td class="col1">描述：</td>
                <td class="col2" id="productDescribe"></td>
                <td class="col1">数量：</td>
                <td class="col2" id="amount"></td>
            </tr>
        </table>

        <table class="table table-bordered" style="margin: 15px auto;">
            <thead>
            <tr>
                <th>序号</th><th>工序名称</th><th>设计工时数</th><th>实际工时数</th>
                <th>设计工时成本（元）</th><th>实际工时成本</th><th>设计物料成本（元）</th>
                <th>实际物料成本</th><th>工序登记</th>
            </tr>
            </thead>
            <tbody id="template">

            </tbody>
        </table>

        <table class="tbl3">
            <tr>
                <td class="col1">设计工时总成本：</td>
                <td class="col2" id="labourCostPriceSum"></td>
                <td class="col1">设计物料总成本：</td>
                <td class="col2" id="moduleCostPriceSum"></td>
            </tr>
            <tr>
                <td class="col1">实际工时总成本：</td>
                <td class="col2" id="realModuleCostPriceSum"></td>
                <td class="col1">实际物料总成本：</td>
                <td class="col2" id="realLabourCostPriceSum"></td>
            </tr>
            <tr>
                <td class="col1">登记人：</td>
                <td class="col2" id="register"></td>
                <td class="col1">登记时间：</td>
                <td class="col2" id="registerTime"></td>
            </tr>
<%--            <tr>--%>
<%--                <td class="col1">审核人：</td>--%>
<%--                <td class="col2" id="checker"></td>--%>
<%--                <td class="col1">审核时间：</td>--%>
<%--                <td class="col2" id="checkTime"></td>--%>
<%--            </tr>--%>
            <tr>
                <td class="col1">备注：</td>
                <td colspan="3" style="padding-top: 5px" id="remark">
                </td>
            </tr>

        </table>
    </div>
</div>

<div class="modal fade" id="proRegistraModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="container-fluid">
                    <div id="designListBox">
                        <div style="text-align: center">
                            <h3>生产登记单</h3>
                        </div>
                        <div style="padding: 30px">
                            <form>
                                <table class="tbl1">
                                    <tr>
                                        <td class="col1">派工单编号：</td>
                                        <td colspan="col2" id="manufactureId2">aaaaaaaaaa</td>
                                        <td class="col1">工序名称：</td>
                                        <td class="col2" id="procedureName"></td>
                                    </tr>
                                    <tr>
<%--                                        <td class="col1">负责人：</td>--%>
<%--                                        <td class="col2"><input type="text" class="txt_under_line" id="register2"></td>--%>
                                        <td class="col1">设计工时数：</td>
                                        <td class="col2" id="labourHourAmount"></td>
                                    </tr>
                                    <tr>
                                        <td class="col1">已用工时数：</td>
                                        <td class="col2" id="realLabourHourAmount"></td>
                                        <td class="col1">本次工时数：</td>
                                        <td class="col2"><input type="text" class="txt_under_line" id="HourAmount"></td>
                                    </tr>
                                </table>

                                <table class="table table-bordered" style="margin: 15px auto;">
                                    <thead>
                                    <tr>
                                        <th>序号</th><th>物料名称</th><th>物料编号</th><th>设计数量</th>
                                        <th>补充数量</th><th>已使用数量</th><th>本次数量</th>
                                    </tr>
                                    </thead>
                                    <tbody id="template2">
                                    <tr>
                                        <td>aaaaaaaaaaaaaaaaaaaa</td>
                                        <td>aaaaaaaaaaaaa</td>
                                        <td>a</td>
                                        <td>a</td>
                                        <td>a</td>
                                        <td>a</td>
                                        <td class="num"><input class="num-input"></td>
                                    </tr>
                                    </tbody>
                                </table>

                                <table class="tbl3">
                                    <tr>
                                        <td class="col1">登记人：</td>
                                        <td colspan="1"><input id="register2" type="text" class="txt_under_line"></td>
                                        <td class="col1">登记时间：</td>
                                        <td colspan="2" id="registerTime2"></td>
                                    </tr>
                                </table>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button id="btnOk1" class="btn btn-success" >
                    未完成<span class="glyphicon glyphicon-remove"></span>
                </button>
                <button id="btnOk2" class="btn btn-success" >
                    已完成<span class="glyphicon glyphicon-remove"></span>
                </button>
                <button id="btnConcel" class="btn btn-danger" data-bs-dismiss="modal">
                    关闭<span class="glyphicon glyphicon-remove"></span>
                </button>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="/js/jquery-3.7.0.min.js"></script>
<script type="text/javascript" src="/bootstrap/bootstrap-5.3.3-dist/js/bootstrap.bundle.js"></script>
<script type="text/javascript">
    $(function (){
        initialTable();

        // 模态框显示时触发
        $("#proRegistraModal").on("show.bs.modal",function (){
            let id = sessionStorage.getItem("id");
            let manufactureId = sessionStorage.getItem("manufactureId");
            let labourHourAmount = sessionStorage.getItem("labourHourAmount");
            let realLabourHourAmount = sessionStorage.getItem("realLabourHourAmount");
            let procedureName = sessionStorage.getItem("procedureName");
            $("#manufactureId2").text(manufactureId);
            $("#labourHourAmount").text(labourHourAmount);
            $("#realLabourHourAmount").text(realLabourHourAmount==""?"0":realLabourHourAmount);
            $("#procedureName").text(procedureName);
            $.ajax({
                type: "GET",
                url: "/m-procedure-module/initialModule?id="+id,
                dataType: "json",
                success: function (data) {
                    if(data!=null){
                        let tbody = $("#template2");
                        tbody.empty();
                        for (let k = 1;k <= data.length;k++){
                            for (let i = 0; i < data.length; i++) {
                                if(data[i].detailsNumber == k){
                                    let moduleSubtotal = 0;
                                    data[i].moduleSubtotal === null? moduleSubtotal = 0 : moduleSubtotal = data[i].moduleSubtotal;
                                    let tr = $("<tr></tr>");
                                    let td0 = $("<td id='"+data[i].id+"'></td>").text(data[i].detailsNumber);
                                    let td1 = $("<td></td>").text(data[i].productName);
                                    let td2 = $("<td id='"+data[i].productId+"'></td>").text(data[i].productId);
                                    let td3 = $("<td></td>").text(data[i].amount);
                                    let td4 = $("<td></td>").text(data[i].renewAmount);
                                    let td5 = $("<td></td>").text(data[i].realAmount);
                                    let td6 = $("<td class='num' style='background-color: #fdffdc'></td>").html("<input class='num-input'>");
                                    tr.append(td0,td1,td2,td3,td4,td5,td6);
                                    tbody.append(tr);
                                }
                            }
                        }
                    }
                }
            })
        })
        $("#HourAmount").on("input",function () {
            let num = $(this).val();
            if (num == "") {
                return;
            }
            // 校验输入的数字是否合法
            if (!/^\d+$/.test(num)) {
                alert("请输入合法的数字！");
                $(this).val("");
                return;
            }
            //判断是否超过设计工时数
            let labourHourAmount = parseInt($("#labourHourAmount").text());
            let realLabourHourAmount = parseInt($("#realLabourHourAmount").text());
            if (num > labourHourAmount) {
                alert("输入的数字不能超过设计工时数！");
                $(this).val("");
                return;
            }
            //判断是否超过可用工时数
            if (num > (parseInt(labourHourAmount)-parseInt(realLabourHourAmount))) {
                alert("输入的数字不能超过可用工时数！");
                $(this).val("");
                return;
            }
        })

        $("#template2").on("input",".num-input",function () {
            let num = $(this).val();
            if (num == "") {
                return;
            }
            // 校验输入的数字是否合法
            if (!/^\d+$/.test(num)) {
                alert("请输入合法的数字！");
                $(this).val("");
                return;
            }
            //判断是否超过设计数量
            let designAmount = parseInt($(this).parent().parent().find("td:eq(3)").text());
            let renewAmount = parseInt($(this).parent().parent().find("td:eq(4)").text());
            let realAmount = parseInt($(this).parent().parent().find("td:eq(5)").text());
            if (num > designAmount) {
                alert("输入的数字不能超过设计数量！");
                $(this).val("");
                return;
            }
            //判断是否超过补充数量
            if (num > renewAmount) {
                alert("输入的数字不能超过补充数量！");
                $(this).val("");
                return;
            }
            //判断是否超过可用数量
            if (num > (parseInt(renewAmount)-parseInt(realAmount))) {
                alert("输入的数字不能超过可用数量！");
                $(this).val("");
                return;
            }
        })
        $("#btnOk1").on("click",function () {
            submit("0","2");
        })
        $("#btnOk2").on("click",function () {
            submit("1","2");
        })
    })
    function submit(tag1,tag2) {
        //判断是否有提交标志
        let flag = sessionStorage.getItem("flag");
        if (flag == "1") {
            alert("已提交，请勿重复提交！");
            return;
        }
        let id = sessionStorage.getItem("id");
        let manufactureId = sessionStorage.getItem("manufactureId");
        let HourAmount = $("#HourAmount").val();
        if (HourAmount == "") {
            alert("请输入工时数！");
            return;
        }
        let register2 = $("#register2").val();
        if (register2 == "") {
            alert("请输入登记人！");
            return;
        }
        let realAmountArr = {};
        let tr = $("#template2 tr")
        for (let i = 0; i < tr.length; i++) {
            let t = tr[i];
            let numInput = t.querySelector(".num-input");
            let realAmount = numInput.value;
            if (realAmount != "") {
                let productId = tr[i].cells[2].id;
                realAmountArr[productId] = realAmount;
            }else {
                let productName = tr[i].cells[1].innerText;
                alert("请输入物料:‘"+productName+"’数量！");
                return;
            }
        }
        let data = {
            id:id,
            tag1:tag1,
            tag2:tag2,
            manufactureId:manufactureId,
            HourAmount:HourAmount,
            register2:register2,
            realAmountArr:realAmountArr
        }
        $.ajax({
            url:"/m-proceduring/registered",
            type:"post",
            data:JSON.stringify(data),
            contentType:"application/json",
            dataType:"json",
            success:function (data) {
                if (data) {
                    alert("提交成功！");
                    $("#proRegistraModal").modal("hide");
                    initialTable();
                }else {
                    alert("提交失败！");
                }
            }
        })
        //存储提交标志
        sessionStorage.setItem("flag","1");
    }
    function initialTable() {
        let id = sessionStorage.getItem("id");
        $.ajax({
            url:"/m-manufacture/selectById?id="+id,
            type:"get",
            dataType:"json",
            success:function (mManufacture) {
                $("#manufactureId").text(mManufacture.manufactureId);
                $("#productId").text(mManufacture.productId);
                $("#productName").text(mManufacture.productName);
                $("#amount").text(mManufacture.amount);
                $("#designer").text(mManufacture.designer);
                $("#labourCostPriceSum").text(mManufacture.labourCostPriceSum);
                $("#moduleCostPriceSum").text(mManufacture.moduleCostPriceSum);
                $("#register").text(mManufacture.register);
                $("#registerTime").text(mManufacture.registerTime);
                $("#remark").text(mManufacture.remark);
                initialTemplate(id);
            }

        })
    }
    function initialTemplate(id){
        $.ajax({
            type: "GET",
            url: "/m-procedure/selectArrByParentId?id="+id,
            dataType: "json",
            success: function (data){
                $("#template").empty()

                for(let k = 1;k <= data.length;k++){
                    for (let i = 0; i < data.length; i++) {
                        if(data[i].detailsNumber == k){
                            let moduleSubtotal = 0;
                            data[i].moduleSubtotal === null ? moduleSubtotal = 0 : moduleSubtotal = data[i].moduleSubtotal;
                            let procedureDescribe = data[i].procedureDescribe != undefined?data[i].procedureDescribe:""
                            let tr = $("<tr></tr>");
                            let td0 = $("<td id='"+data[i].id+"'></td>").text(data[i].detailsNumber);
                            let td1 = $("<td></td>").text(data[i].procedureName);
                            let td2 = $("<td></td>").text(data[i].labourHourAmount);
                            let td3 = $("<td></td>").text(data[i].realLabourHourAmount);
                            let td4 = $("<td></td>").text(data[i].subtotal);
                            let td5 = $("<td></td>").text(data[i].realSubtotal);
                            let td6 = $("<td></td>").text(data[i].moduleSubtotal);
                            let td7 = $("<td></td>").text(data[i].realModuleSubtotal);
                            let obj = data[i];
                            let td8 =""
                            if(obj.procedureFinishTag == "0"){
                                if(obj.procedureTransferTag == "0"){
                                    td8 = $("<td></td>").html("<a href='javascript:registered("+data[i].id+")' class='btn btn-primary'>登记</a>");
                                }else if(obj.procedureTransferTag == "2"){
                                    td8 = $("<td></td>").html("<span style='background-color: lightcoral;width: 80%;height: 80%'>待审核</span>");
                                }else if(obj.procedureTransferTag == "3"){
                                    td8 = $("<td></td>").html("<a href='javascript:registered("+data[i].id+")' class='btn btn-primary'>登记</a>");
                                }
                            }else if(obj.procedureFinishTag == "1"){
                                if(obj.procedureTransferTag == "0"){
                                    td8 = $("<td></td>").html("<a href='javascript:handover("+data[i].id+")' class='btn btn-primary'>交接</a>");
                                }else if(obj.procedureTransferTag == "2"){
                                    td8 = $("<td></td>").html("<span style='background-color: lightcoral;width: 80%;height: 80%'>待审核</span>");
                                }else if(obj.procedureTransferTag == "1"){
                                    td8 = $("<td></td>").html("<span style='background-color: #42b576'>已交接</span>");
                                }
                            }else if(obj.procedureFinishTag == "2"){
                                if(obj.procedureTransferTag == "1"){
                                    td8 = $("<td></td>").html("<span style='background-color: lightcoral;width: 80%;height: 80%'>待审核</span>");
                                }
                            }
                            tr.append(td0,td1,td2,td3,td4,td5,td6,td7,td8);
                            $("#template").append(tr);
                        }
                    }
                }
            }
        })
    }
    function registered(id) {
        let td = $("#template").find("td[id='"+id+"']");
        let val = td.parent().prev().find("td:eq(8)").text()
        if(val == "登记"){
            alert("前置工序未完成，请先完成前置工序交接后再登记！")
            return;
        }
        if (val == "待审核"){
            alert("前置工序未完成，请先完成前置工序交接后再登记！")
            return;
        }
        if (val == "交接"){
            alert("前置工序未完成，请先完成前置工序交接后再登记！")
            return;
        }
        let manufactureId = $("#manufactureId").text();
        let labourHourAmount =td.parent().find("td:eq(2)").text()
        let realLabourHourAmount = td.parent().find("td:eq(3)").text()
        sessionStorage.setItem("id",id);
        sessionStorage.setItem("manufactureId",manufactureId);
        sessionStorage.setItem("labourHourAmount",labourHourAmount);
        sessionStorage.setItem("realLabourHourAmount",realLabourHourAmount);
        sessionStorage.setItem("procedureName",td.parent().find("td:eq(1)").text())
        $("#proRegistraModal").modal("show")
    }
    function handover(id) {
        let a = confirm("确认交接吗？")
        if (a){
            $.ajax({
                url:"/m-proceduring/handover?id="+id,
                type:"get",
                dataType:"json",
                success:function (data) {
                    if (data) {
                        alert("交接成功！");
                        $("#proRegistraModal").modal("hide");
                        initialTable();
                    }else {
                        alert("交接失败！");
                    }
                }
            })
        }


    }

</script>
</body>
</html>